package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.model.Member;
import com.qy.model.RewardOrder;
import com.qy.service.MemberService;
import com.qy.service.RewardOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.util.Date;
import java.util.List;

/**
* Created by zaq on 2018/07/10.
*/
@RestController("Manage-RewardOrderController")
@RequestMapping("/manage/reward/order")
public class RewardOrderController {
    @Resource
    private RewardOrderService rewardOrderService;
    @Resource
    private MemberService memberService;

    @PostMapping("/add")
    public Result add(@RequestBody RewardOrder rewardOrder) {
        rewardOrderService.save(rewardOrder);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        rewardOrderService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody RewardOrder rewardOrder) {
        rewardOrderService.update(rewardOrder);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        RewardOrder rewardOrder = rewardOrderService.findById(id);
        return ResultGenerator.successResult(rewardOrder);
    }

    @GetMapping("/list")
    public Result list(PageBean<RewardOrder> page) {
        PageHelper.startPage(page.getPageNum(),page.getSize());
        List<RewardOrder> list = rewardOrderService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

//    悬赏订单列表
    @GetMapping("/selectRewardOrderList")
    public Result selectRewardOrderList(PageBean<RewardOrder> page,Integer type ,String content ,RewardOrder rewardOrder){
        if (type ==1){
            rewardOrder.setOrder_number(content);
        }else if (type ==2){
            rewardOrder.setRewarder(content);
        }else if (type ==3){
            rewardOrder.setPhone(content);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize() ,"submit_time desc");
        List<RewardOrder> list =rewardOrderService.selectRewardOrderList(rewardOrder);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

//    悬赏订单-详情查看
    @GetMapping("/seeRewardOrder")
    public Result seeRewardOrder(Integer id){
        RewardOrder rewardOrder = rewardOrderService.findById(id);
        return ResultGenerator.successResult(rewardOrder);
    }

//    悬赏订单详情-回复
    @PostMapping("/reply")
    public Result reply(@RequestBody RewardOrder rewardOrder){
        if (rewardOrder.getId() == null){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        if (rewardOrder.getReply_content() == null || rewardOrder.getReply_content().isEmpty()){
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (rewardOrder.getExamine_state() == null){
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (rewardOrder.getExamine_state() == 2){
            //审核通过  给该提供者添加免费条数5条
           RewardOrder rewardOrder1 = rewardOrderService.findById(rewardOrder.getId());
            Member member = memberService.findById(rewardOrder1.getUser_id());
            member.setMember_surplus_number(member.getMember_surplus_number()+5);
            memberService.update(member);
        }
        rewardOrder.setReply_time(String.valueOf(new Date().getTime()));
        rewardOrderService.update(rewardOrder);
        return ResultGenerator.successResult();
    }
}
